<?php 

header('content-type: application/json; charset=utf-8');

$database = "learner_profile";

$table = "teacher";

$default_apikey = "d6da2a5570f2dee467be31ced7339e3e";

$json_array = array("status"=>"error","error_id"=>"post_not_set","error_description"=>"no request data received");

if(isset($_POST["submit"]) and !empty($_POST["apikey"]) and !empty($_POST["username"]) and !empty($_POST["password"]))
{
	$apikey = $_POST["apikey"];

	$username = $_POST["username"];

	$password = $_POST["password"];
		
	if($apikey == $default_apikey)
	{
		
		$conn = mysql_connect("localhost","root","") or die("Couldn't connect to database");

		$db = mysql_select_db($database) or die("Couldn't connect to database");
		
		$query = sprintf("select firstname, lastname, teacherID from %s where email='%s' and password='%s';", $table, $username, $password);

		$result = mysql_query($query) or die("Query_failed: " . mysql_error());

		$num_of_rows = mysql_num_rows($result);
		
		/*pick only the first row, as there should be only one row*/
		if($num_of_rows == 1)
		{
			$row = mysql_fetch_array($result);
				
			$firstname = $row["firstname"];
	
			$lastname = $row["lastname"];
			
			$teacherID = $row['teacherID'];
		
			/*set the session id values, token needs to be clearified*/
			$session_token = 'Some token';
			$_session["id"] = $teacherID;
	
	    	/*create json representation*/
			$json_array = array('status'=>'OK', 'session_token'=>sprintf('<%s>',$session_token), 'id'=>sprintf('<%s>',$teacherID));
		}
		else{
				$json_array = array('status'=>'error','error_id'=>'userpassword','error_description'=>'username or password not found');
			}
		}
	else{
		$json_array = array('status'=>'error','error_id'=>'apikey','error_description'=>'apikey error');
	}

}

$json_ret = json_encode($json_array);

echo $json_ret;

?>